from django.shortcuts import HttpResponse,redirect,render
from django.http import JsonResponse
from web.forms.account import RegisterModelFrom,SendSmsForm

def register(request):
    """注册"""
    if request.method == 'GET':
        form = RegisterModelFrom()
        return render(request,'register.html',{'form':form})
    # print(request.POST)
    form = RegisterModelFrom(data=request.POST)
    if form.is_valid():
        # 验证通过，写入数据库(密码要是密文)
        form.save()
        return JsonResponse({'status': True,'data':'/login/'})

    return JsonResponse({'status': False,'error':form.errors})

def send_sms(request):
    """发送短信"""

    form = SendSmsForm(request,data=request.GET)
    # 只是校验qq邮箱不能为空，格式是否正确
    # print("request=",request.GET.get('email'))
    if form.is_valid():
        return JsonResponse({'status': True})
    return JsonResponse({'status': False, 'error': form.errors})